package com.wanpinghui.wphlibrary.model.message.bean;

import com.google.gson.Gson;
import com.wanpinghui.wphlibrary.model.db.manager.MsgDbManager;
import com.wanpinghui.wphlibrary.model.oauth.UserInfoUtil;
import com.wanpinghui.wphlibrary.model.oauth.bean.UserBean;
import com.wanpinghui.wphlibrary.model.oauth.bean.UserInformation;
import com.yunshl.yunshllibrary.utils.TextUtil;

import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by xiongning on 17-12-4.
 */
@Table(name = "im_msg")
public class MsgBean {

    //指明字段,主键,是否自增长,约束(不能为空)
     @Column(name = "id", isId = true, autoGen = false, property = "NOT NULL")
     private String id;

    @Column(name = "account")
    private String account;

     @Column(name = "msg")
     private String msg; // 消息内容

    @Column(name = "type")
    private int type;//0

     @Column(name = "from_id")
     private String from_id;// 发送人

     @Column(name = "msg_type")
     private int msg_type;// 消息类型

     @Column(name = "to_id")
     private String to_id;//接收人

     @Column(name = "created_at")
     private long created_at;// 消息发送的时间戳

     @Column(name = "send_statu")
     private int send_statu;// 0:seding,1:success,2:failed消息发送是否成功

     @Column(name = "direct")
     private int direct;//0:out 1:in

     @Column(name = "ext")
     private String ext;//ext info

     private String message;
    private int message_id;

    public MsgBean() {
    }

    public void processRcvMsgBean(){
        this.send_statu=1;
        if(from_id.equals(getMyImId())){
            this.direct=0;//out
            this.account=to_id;
        }
        if(to_id.equals(getMyImId())){
            this.direct=1;//in
            this.account=from_id;
        }
        this.id=account+":"+created_at;
        if(TextUtil.isNotEmpty(message)){
            Messages messages=Messages.getMessages(message);
            this.ext=messages.getExt();
            this.type=messages.getType();
            this.msg=messages.getMsg();
        }
    }

    public MsgBean(String account, String msg,String ext,int type,int msg_type, long created_at, int direct) {
        if(direct==0){//0:out
            this.from_id=getMyImId();
            this.to_id=account;
            this.send_statu=0;
        }else {//1:in
            this.to_id=getMyImId();
            this.from_id=account;
            this.send_statu=1;
        }
        this.account=account;
        this.id=account+":"+created_at;
        this.msg = msg;
        this.type = type;
        this.msg_type=msg_type;
        this.created_at = created_at;
        this.direct = direct;
        this.ext=ext;
        Messages m=new Messages(type,msg,ext);
        this.message=new Gson().toJson(m);
    }

    public static String getGroupId(String chatIm,int msg_type){
        List<String> ids=new ArrayList<>();
        ids.add(getMyImId());
        ids.add(chatIm);
        ids.add((msg_type/10)+"");
        Collections.sort(ids, new Comparator<String>() {
            @Override
            public int compare(String lhs, String rhs) {
                return lhs.compareTo(rhs);
            }
        });
        String groupId=ids.get(0)+":"+ids.get(1)+":"+ids.get(2);
        return groupId;
    }

    public String getGroupId(){
        List<String> ids=new ArrayList<>();
        ids.add(from_id);
        ids.add(to_id);
        ids.add((msg_type/10)+"");
        Collections.sort(ids, new Comparator<String>() {
            @Override
            public int compare(String lhs, String rhs) {
                return lhs.compareTo(rhs);
            }
        });
        String groupId=ids.get(0)+":"+ids.get(1)+":"+ids.get(2);
        return groupId;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getSend_statu() {
        return send_statu;
    }

    public void setSend_statu(int send_statu) {
        this.send_statu = send_statu;
    }

    public long getCreated_at() {
        return created_at;
    }

    public void setCreated_at(long created_at) {
        this.created_at = created_at;
    }

    public int getMessage_id() {
        return message_id;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFrom_id() {
        return from_id;
    }

    public void setFrom_id(String from_id) {
        this.from_id = from_id;
    }

    public String getTo_id() {
        return to_id;
    }

    public void setTo_id(String to_id) {
        this.to_id = to_id;
    }

    public int getDirect() {
        return direct;
    }

    public void setDirect(int direct) {
        this.direct = direct;
    }

    public static String getMyImId(){
        String myImId=null;
        UserBean ub= UserInfoUtil.getUserInfo();
        if(ub!=null){
            UserInformation uinf=ub.getUser();
            if(uinf!=null){
                myImId=ub.getUser().getId();
            }
        }
        return myImId;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getMsg() {
        if(TextUtil.isEmpty(msg))
            initMessage();
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getMsg_type() {
        return msg_type;
    }

    public void setMsg_type(int msg_type) {
        this.msg_type = msg_type;
    }

    public String getExt() {
        return ext;
    }

    public void setExt(String ext) {
        this.ext = ext;
    }

    public void initMessage(){
        if(TextUtil.isNotEmpty(message)){
            Messages m=new Gson().fromJson(message,Messages.class);
            this.msg=m.getMsg();
            this.ext=m.getExt();
            this.type=m.getType();
        }
    }

    @Override
    public String toString() {
        return "MsgBean [id="+id+",message="+message+",ext="+ext+",type="+type+",from_id="+from_id+",msg_type="+msg_type+",to_id="+to_id+",created_at="+created_at+",send_statu="+send_statu+",direct="+direct+"]";
    }
}
